﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace BadHabit.Factory.Views {
    /// <summary>
    /// Interaction logic for SprintBreak.xaml
    /// </summary>
    public partial class SprintBreak : BadHabit.WPFTools.ShapedWindow {


        public static DependencyProperty BreakHoursProperty = DependencyProperty.Register("BreakHours", typeof(int), typeof(SprintBreak));
        public static DependencyProperty SprintNumberProperty = DependencyProperty.Register("SprintNumber", typeof(int), typeof(SprintBreak));
        public static DependencyProperty SprintLengthProperty = DependencyProperty.Register("SprintLength", typeof(int), typeof(SprintBreak));

        /// <summary>
        /// Gets or sets the nsource of the image to render.
        /// </summary>
        public int BreakHours {
            get { return (int)GetValue(BreakHoursProperty); }
            set { SetValue(BreakHoursProperty, value); }
        }

        /// <summary>
        /// Gets or sets the nsource of the image to render.
        /// </summary>
        public int SprintNumber {
            get { return (int)GetValue(SprintNumberProperty); }
            set { SetValue(SprintNumberProperty, value); }
        }

        /// <summary>
        /// Gets or sets the nsource of the image to render.
        /// </summary>
        public int SprintLength {
            get { return (int)GetValue(SprintLengthProperty); }
            set { SetValue(SprintLengthProperty, value); }
        }


        private Data.ProjectDataSet dsProject;

        public SprintBreak(Data.ProjectDataSet dsProject) {
            InitializeComponent();
            this.dsProject = dsProject;
            BreakHours = 120;
            SprintNumber = 2;
            SprintLength = 15;
        }   
      
      

      

        private void Save_Executed(object sender, ExecutedRoutedEventArgs e) {
            BreakIt();
            this.Close();
        }

        private void Undo_Executed(object sender, ExecutedRoutedEventArgs e) {            
            this.Close();
        }

        private void BreakIt() {
            if (dsProject.Project.Count == 0)
                return;

            Data.ProjectDataSet dsSprintItems = null;
            if (defaultItemsCheckBox.IsChecked == true) {
                dsSprintItems = new Data.ProjectDataSet();
                dsSprintItems.EnforceConstraints = false;
                try {
                    dsSprintItems.Merge(Client.Service.RetriveSprintDefaultItems(dsProject.Project[0].Category));
                } catch (Exception ex) {
                    Views.Error.ShowError(ex, "Sorry, could not retrive sprint default items");
                    return;
                }
            }

            if(sprintDurationRadio.IsChecked==true)
                dsProject.Project[0].BreakLastSprintByHour( (double) BreakHours, SprintLength, dsSprintItems);

            if (sprintNumberRadio.IsChecked == true) {
                dsProject.Project[0].BreakLastSprintByHour(CalculatedSprintMaxHours, SprintLength, dsSprintItems);
            }

        }



        private double CalculatedSprintMaxHours {
            get {
                double totalHours = (double)dsProject.Project[0]["TotalHours"];
                if (SprintNumber == 0)
                    return 1;
                double max = totalHours / SprintNumber;
                if (max < 1)
                    max = 1;
                return max;
            }
        }

        private void ShapedWindow_Loaded(object sender, RoutedEventArgs e) {
            Client.FactoryMainWindow.IsPopUpOpened = true;
        }

        protected override void OnClosed(EventArgs e) {
            base.OnClosed(e);
            if (Client.FactoryMainWindow != null)
                Client.FactoryMainWindow.IsPopUpOpened = false;
        }

        private void Save_CanExecute(object sender, CanExecuteRoutedEventArgs e) {
            e.CanExecute = 
             !(sprintHoursTextBox.GetBindingExpression(TextBox.TextProperty).HasError ||
             sprintLengthTextBox.GetBindingExpression(TextBox.TextProperty).HasError ||
             sprintLengthTextBox.GetBindingExpression(TextBox.TextProperty).HasError);
        }

    }
}
